using System;
using System.Collections.Generic;
using System.Text;
using FlatRedBall;
using FlatRedBall.Input;
using FlatRedBall.AI.Pathfinding;
using FlatRedBall.Graphics.Animation;
using FlatRedBall.Graphics.Particle;

using FlatRedBall.Math.Geometry;
using FlatRedBall.Math.Splines;
using BitmapFont = FlatRedBall.Graphics.BitmapFont;
using Cursor = FlatRedBall.Gui.Cursor;
using GuiManager = FlatRedBall.Gui.GuiManager;
using Microsoft.Xna.Framework.Graphics;

#if FRB_XNA || SILVERLIGHT
using Keys = Microsoft.Xna.Framework.Input.Keys;
using Vector3 = Microsoft.Xna.Framework.Vector3;
using Texture2D = Microsoft.Xna.Framework.Graphics.Texture2D;


#endif

namespace FutureWar.Entities
{
	public partial class StatusBar
	{
        bool changed;

        enum LogicState 
        { 
            Normal,
            Flashing,
            Invisible
        }

        enum ColorState 
        { 
            Normal,
            Danger,
            Warning
        }

        LogicState logicState;
        ColorState colorState;
        ColorState oldState;
        float value;
        float max;
        Color danger = Color.Red;
        Color warning = Color.Yellow;
        Color normal = Color.Green;

        public void SetColors(Color danger, Color warning, Color normal) 
        {
            this.danger = danger;
            this.warning = warning;
            this.normal = normal;
        }

        public void SetValue(float value)
        {
            if (this.value != value)
            {
                changed = true;
                if (value > max)
                    this.value = max;
                else if (value < 0)
                    this.value = 0;
                else
                    this.value = value;
            }
                    
        }

        public void ChangeValue(float value)
        {
            if (value != 0)
            {
                changed = true;
                this.value += value;

                if (this.value > max)
                    this.value = max;
                else if (this.value < 0)
                    this.value = 0;
            }
            
        }

        public float MaxHealth
        {
            get { return max; }
            set { max = value; }
        }

		private void CustomInitialize()
        {

            ChangeColor(normal);

            pixelsPerUnit = SpriteManager.Camera.PixelsPerUnitAt(Z);
            
            this.ParentRotationChangesRotation = false;
            this.ParentRotationChangesPosition = false;

            Value.ScaleY = 2f / pixelsPerUnit;
            Background.ScaleY = 2f / pixelsPerUnit;
            Frame.ScaleY = 3f / pixelsPerUnit;

            Background.ScaleX = Width;
            Frame.ScaleX = Width + (1f / pixelsPerUnit);
            
            max = 100;
            value = max;
		}

		private void CustomActivity()
        {
            if (changed)
            {
                DrawBar();
                changed = false;
            }
		}

        float pixelsPerUnit;
        private void DrawBar()
        {
            if (value == max)
                CurrentState = VariableState.Transparent;
            else
            {
                Value.ScaleX = value * (Width / max);
                Value.RelativePosition.X = -(max - value) * (Width / max);

                if (value < max * 0.25f)
                {
                    colorState = ColorState.Danger;
                    logicState = LogicState.Flashing;
                }
                else if (value < max * 0.50f)
                {
                    colorState = ColorState.Warning;
                    logicState = LogicState.Normal;
                }

                else if (value < max * 0.75f)
                {
                    colorState = ColorState.Normal;
                    logicState = LogicState.Normal;
                }
                else
                {
                    colorState = ColorState.Normal;
                    logicState = LogicState.Normal;
                }


                switch (logicState)
                {
                    case LogicState.Normal:
                        InterpolateToState(VariableState.Opaque, 1);
                        break;

                    case LogicState.Invisible:
                        InterpolateToState(VariableState.Transparent, 1);
                        break;

                    case LogicState.Flashing:
                        if (CurrentState == VariableState.Opaque)
                            InterpolateToState(VariableState.Transparent, .2);
                        if (CurrentState == VariableState.Transparent)
                            InterpolateToState(VariableState.Opaque, .2);
                        break;
                }

                if (colorState != oldState)
                {
                    switch (colorState)
                    {
                        case ColorState.Normal:
                            ChangeColor(normal);
                            break;

                        case ColorState.Warning:
                            ChangeColor(warning);
                            break;

                        case ColorState.Danger:
                            ChangeColor(danger);
                            break;
                    }
                }
                oldState = colorState;
            } 
        }

        private void ChangeColor(Color color)
        {
            Value.Red = color.R/255f;
            Value.Green = color.G/255f;
            Value.Blue = color.B/255f;
        }

        private static void CustomLoadStaticContent(string contentManagerName)
        {


        }

        private void CustomDestroy()
        {


        }
	}
}
